package com.kly.smartbi.controller.bi;

import cn.hutool.core.util.ObjUtil;
import com.kly.smartbi.common.BaseResponse;
import com.kly.smartbi.common.ErrorCode;
import com.kly.smartbi.common.ResultUtils;
import com.kly.smartbi.exception.ThrowUtils;
import com.kly.smartbi.model.dto.picture.PictureQueryRequest;
import com.kly.smartbi.model.dto.picture.PictureQueryResult;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author admin
 * @date 2025/4/14
 */
@RequestMapping("/pictures")
@RestController
@Slf4j
public class PictureController {


    private static final String URL = "https://www.bing.com/images/async?q=%s&first=%s&count=%s&mmasync=1";


    @GetMapping("/search")
    public BaseResponse<List<PictureQueryResult>> searchPictures(PictureQueryRequest pictureQueryRequest) {
        String searchText = pictureQueryRequest.getSearchText();
        int current = pictureQueryRequest.getCurrent();
        int pageSize = pictureQueryRequest.getPageSize();
        int first = (current - 1) * pageSize;
        String fetchUrl = String.format(URL, searchText, first, pageSize);
        Document document = null;
        try {
            document = Jsoup.connect(fetchUrl).get();
            //     获取dg_b类
        } catch (IOException e) {
            log.error("searchPictures error", e);
            e.printStackTrace();
        }

        // 创建结果类
        List<PictureQueryResult> pictureQueryResultList = new ArrayList<>();
        ThrowUtils.throwIf(ObjUtil.isNull(document), ErrorCode.NOT_FOUND_ERROR);
        Elements dgB = document.getElementsByClass("dg_b");
        ThrowUtils.throwIf(ObjUtil.isNull(dgB), ErrorCode.NOT_FOUND_ERROR);
        Elements iuscp = dgB.select("div.iuscp");
        ThrowUtils.throwIf(ObjUtil.isNull(iuscp), ErrorCode.NOT_FOUND_ERROR);
        iuscp.forEach(ius -> {
            // 创建结果
            PictureQueryResult pictureQueryResult = new PictureQueryResult();
            Elements img = ius.select("img.mimg");
            ThrowUtils.throwIf(ObjUtil.isNull(img), ErrorCode.NOT_FOUND_ERROR);
            String src = img.attr("src");
            pictureQueryResult.setSrc(src);
            Elements b_dataList = ius.select("ul.b_dataList");
            ThrowUtils.throwIf(ObjUtil.isNull(b_dataList), ErrorCode.NOT_FOUND_ERROR);
            Elements a = b_dataList.select("a");
            ThrowUtils.throwIf(ObjUtil.isNull(a), ErrorCode.NOT_FOUND_ERROR);
            String title = a.attr("title");
            pictureQueryResult.setTitle(title);
            pictureQueryResultList.add(pictureQueryResult);
        });
        return ResultUtils.success(pictureQueryResultList);
    }
}
